package service;


import dao.TokenDao;
import entity.Token;
import entity.User;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.util.UUID;

/**
 * @program: SSM201759225309
 * @description: 用于提供Token操作
 * @author: Xiaozhuoer
 * @create: 2021-05-15 19:19
 **/

@Component
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class TokenService implements TokenDao {

    @Autowired
    private RedisService redisService; //提供redis相关服务

    public TokenService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * 生成Token
     * @param user 用户类
     * @return token
     */
    @Override
    public Token CreateToken(User user){
        Token token = new Token();
        //tokenString: type-id-username-password-email
        StringBuilder tokenString = new StringBuilder();
        //设置type 0:管理者 1:开发者 2:普通用户 3:设备
        tokenString.append(user.toTokenString());
        token.getType(user.getType());
        //设置UUID
        token.setUuid(UUID.randomUUID().toString());
        //存入redis,过期时间10秒
        redisService.set(token.getUuid(), tokenString.toString(), 1000);
        return token;
    }


    /**
     * 鉴权
     * @param uuid
     * @return 无0-有1
     */
    public boolean CheckToken(String uuid){
        return redisService.hasKey(uuid);
    }

    /**
     * 修改value
     * @param key, newValue
     */
    public void updateValue(String key,User user){
        String newValue = user.toTokenString();
        redisService.set(key, newValue, 1000);
    }

    /**
     * 根据uuid获取存于redis中的tokenString
     * @param uuid
     * @return
     */
    public String getTokenString(String uuid){
        return redisService.get(uuid);
    }
    /**
     * 分析TokenString获取其中的Type
     * @param tokenString
     * @return String
     */
    public String getTypeByTokenString(@NotNull String tokenString){
        String[] strArg = tokenString.split("-");
        String strTemp = strArg[0];
        String strResult = strTemp.substring(5);
        return strResult;
    }
    /**
     * 分析TokenString获取其中的Id
     * @param tokenString
     * @return String
     */
    public String getIdByTokenString(@NotNull String tokenString){
        String[] strArg = tokenString.split("-");
        String strTemp = strArg[1];
        String strResult = strTemp.substring(3);
        return strResult;
    }
    /**
     * 分析TokenString获取其中的Username
     * @param tokenString
     * @return String
     */
    public String getUserNameByTokenString(@NotNull String tokenString){
        String[] strArg = tokenString.split("-");
        String strTemp = strArg[2];
        String strResult = strTemp.substring(9);
        return strResult;
    }
    /**
     * 分析TokenString获取其中的PassWord
     * @param tokenString
     * @return String
     */
    public String getPassWordByTokenString(@NotNull String tokenString){
        String[] strArg = tokenString.split("-");
        String strTemp = strArg[3];
        String strResult = strTemp.substring(10);
        return strResult;
    }
    /**
     * 分析TokenString获取其中的Email
     * @param tokenString
     * @return String
     */
    public String getEmailByTokenString(@NotNull String tokenString){
        String[] strArg = tokenString.split("-");
        String strTemp = strArg[4];
        String strResult = strTemp.substring(6);
        return strResult;
    }
}
